package com.xy.system.source.act;

import com.xy.system.source.IDataSourceServer;
import com.xy.system.source.vo.DataSourceVO;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MysqlAction implements IDataSourceServer {

    public String getServerName() {
        return "com.mysql.jdbc.Driver";
    }

    public void fillDbName(DataSourceVO cond) {
        ////jdbc:mysql://192.168.0.244:3308/hrms?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true&nullCatalogMeansCurrent=true
        String url = cond.getUrl();
        String newUrl = url.replaceAll("\\\\", "/");
        cond.setUrl(newUrl);
        int end = newUrl.indexOf("?");
        if (end < 0) {
            throw new RuntimeException("url错误");
        }
        String tempUrl = newUrl.substring(0, end);
        int begin = tempUrl.lastIndexOf("/");
        if (begin < 0) {
            throw new RuntimeException("url错误");
        }
        String name = tempUrl.substring(begin + 1).trim();
        cond.setDbName(name);
    }

    @Override
    public List<String> getAllTableMap(Connection conn, DataSourceVO dataSource) {
        List<String> tableNames = new ArrayList<>();
        Statement st = null;
        ResultSet rs = null;
        try {
            st = conn.createStatement();
            StringBuilder sql = new StringBuilder();
            sql.append(" SELECT ");
            sql.append("    table_name      AS table_name ");
            sql.append(" FROM  information_schema.tables ");
            sql.append(" WHERE table_schema = '").append(dataSource.getDbName()).append("' ");
            rs = st.executeQuery(sql.toString());
//            rs = st.executeQuery("SELECT table_name  FROM information_schema.tables WHERE table_schema='" + dataSource.getDbName() + "'");
            ResultSetMetaData rsMetaData = rs.getMetaData();
            while (rs.next()) {
                tableNames.add(rs.getString(1));
            }
            return tableNames;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (st != null) {
                try {
                    st.close();
                } catch (Exception e1) {

                }
            }
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e2) {

                }
            }
        }

        return null;
    }

    @Override
    public Map<String, String> getRemarkNameMap(Connection conn, DataSourceVO dataSource, String tableName) {
        Map<String, String> remarkMap = new HashMap<>();
        Statement st = null;
        ResultSet rs2 = null;
        try {
            st = conn.createStatement();
            StringBuilder sql = new StringBuilder();
            sql.append(" SELECT ");
            sql.append("    column_name      AS column_name ");
            sql.append("    ,column_comment    AS column_comment ");
            sql.append(" FROM  information_schema.columns ");
            sql.append(" WHERE table_name = '").append(tableName).append("' ");
            sql.append("   AND table_schema = '").append(dataSource.getDbName()).append("' ");
            rs2 = st.executeQuery(sql.toString());
//            rs2 = st.executeQuery("SELECT column_name,column_comment FROM information_schema.columns WHERE table_name ='" + tableName + "' AND table_schema='" + dataSource.getDbName() + "'");
            while (rs2.next()) {
                remarkMap.put(rs2.getString(1), rs2.getString(2));
            }
            return remarkMap;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (st != null) {
                try {
                    st.close();
                } catch (Exception e1) {

                }
            }
            if (rs2 != null) {
                try {
                    rs2.close();
                } catch (Exception e2) {

                }
            }
        }

        return null;
    }
}
